<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ME.aspx.cs" Inherits="OnlineAnadolu.ME" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Configuration" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script runat=server>        
        
        protected void Node_Populate(object sender, TreeNodeEventArgs e)
        {
            if (e.Node.ChildNodes.Count == 0)
            {
                switch (e.Node.Depth)
                {
                    case 0:
                        FillAuthors(e.Node);
                        break;
                    case 1:
                        FillTitlesForAuthors(e.Node);
                        break;
                }
            }

        }

        void FillAuthors(TreeNode node)
        {
            string connString = System.Configuration.ConfigurationSettings.AppSettings["MyCon"];
            SqlConnection connection = new SqlConnection(connString);
            SqlCommand command = new SqlCommand("Select * From categories where parentid is null", connection);
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataSet authors = new DataSet();
            adapter.Fill(authors);
            if (authors.Tables.Count > 0)
            {
                foreach (DataRow row in authors.Tables[0].Rows)
                {
                    TreeNode newNode = new TreeNode();
                    newNode.Text = row["categoryname"].ToString();
                    newNode.Value = (row["categoryid"]).ToString();

                    newNode.NavigateUrl = "../UrunGoster.aspx?CatID=" + (row["categoryid"]).ToString();

                    newNode.SelectAction = TreeNodeSelectAction.Expand;
                    node.ChildNodes.Add(newNode);

                    FillTitlesForAuthors(newNode);
                    
                }
            }
        }

        void FillTitlesForAuthors(TreeNode node)
        {
            string authorID = node.Value.ToString();
            string connString = System.Configuration.ConfigurationSettings.AppSettings["MyCon"];
            SqlConnection connection = new SqlConnection(connString);
            SqlCommand command = new SqlCommand("Select categoryname, categoryid From categories Where parentid = '" + authorID + "'", connection);
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataSet titlesForAuthors = new DataSet();
            adapter.Fill(titlesForAuthors);
            if (titlesForAuthors.Tables.Count > 0)
            {
                foreach (DataRow row in titlesForAuthors.Tables[0].Rows)
                {
                    TreeNode newNode = new TreeNode();
                    newNode.Text = row["categoryname"].ToString();
                    newNode.Value = (row["categoryid"]).ToString();
                    newNode.NavigateUrl = "../UrunGoster.aspx?CatID=" + (row["categoryid"]).ToString();

                    newNode.PopulateOnDemand = false;
                    newNode.SelectAction = TreeNodeSelectAction.None;
                    node.ChildNodes.Add(newNode);
                    //node.NavigateUrl = "../UrunGoster.aspx?CatID=" + (row["categoryid"]).ToString();

                    FillTitlesForAuthors(newNode);
                }
            }
        }


    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        &nbsp;<asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource>
        &nbsp; &nbsp; &nbsp; &nbsp;
    
    </div>
        <asp:TreeView ID="tvwauthors" runat="Server" OnTreeNodePopulate="Node_Populate" NodeIndent="10" ShowExpandCollapse="False" >
            <Nodes>
                <asp:TreeNode PopulateOnDemand="True" Text="Anadolu Bazaar" Value="Authors"></asp:TreeNode>
            </Nodes>
            <ParentNodeStyle Font-Bold="False" />
            <HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
            <SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD" HorizontalPadding="0px"
                VerticalPadding="0px" />
            <NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" HorizontalPadding="0px"
                NodeSpacing="0px" VerticalPadding="0px" />
        </asp:TreeView>
        <asp:Menu ID="Menu1" runat="server" MaximumDynamicDisplayLevels="200" BackColor="#F7F6F3" DynamicHorizontalOffset="2" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#7C6F57" Height="31px" OnMenuItemClick="click" StaticSubMenuIndent="10px" Width="203px">
            <Items>
                <asp:MenuItem Text="Kategoriler" Value="anadolu"></asp:MenuItem>
            </Items>
            <StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
            <DynamicHoverStyle BackColor="#7C6F57" ForeColor="White" />
            <DynamicMenuStyle BackColor="#F7F6F3" />
            <StaticSelectedStyle BackColor="#5D7B9D" />
            <DynamicSelectedStyle BackColor="#5D7B9D" />
            <DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
            <StaticHoverStyle BackColor="#7C6F57" ForeColor="White" />
        </asp:Menu>
        <asp:Label ID="Label1" runat="server"></asp:Label>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server"></asp:SqlDataSource>
        <asp:Panel ID="Panel1" runat="server" Height="50px" Width="125px">
        </asp:Panel>
    </form>
</body>
</html>
